import { ref, nextTick, Ref } from "vue";
import { PAGE, PAGE_SIZE } from "@/constant/index";

export function useTable(tableRef: any) {
  const usePage = ref(PAGE);
  const useSize = ref(PAGE_SIZE);

  /** loading */
  const useLoading = (value: boolean, tableElem?: Ref<HTMLElement>): void => {
    const ref = tableElem?.value || tableRef?.value;
    ref?.setLoading(value);
  };

  /** 设置页码 */
  const useSetTotal = (value: number): void => {
    tableRef.value?.setTotal(value * 1);
  };

  /** 设置单元格内容主题 */
  const useSetCellInnerPrimary = (field: string, item: any[]) =>
    item.includes(field) ? "color-primary" : null;

  /**
   * 设置高度
   */
  const useSetHeight = async () => {
    await nextTick();
    tableRef.value?.setHeight();
  };

  return {
    usePage,
    useSize,
    useSetTotal,
    useLoading,
    useSetCellInnerPrimary,
    useSetHeight,
  };
}
